<template>
  <div class="container">
    <div class="head">
      <span class="left">购物车</span>
      <div class="right">
        <div class="take">
          <span class="txt" v-show="!mode">领券</span>
        </div>
        <span class="cart-btn" @click="moning" v-show="!mode">编辑</span>
        <span class="cart-btn" @click="moded" v-show="mode">完成</span>
      </div>
    </div>
    <div class="body">
      <ul class="list">
        <li class="item">
          <i class="iconfont icon-dian"></i>
          <span class="zi">30天无忧退货</span>
        </li>
        <li class="item">
          <i class="iconfont icon-dian"></i>
          <span class="zi">48小时快速退货</span>
        </li>
        <li class="item">
          <i class="iconfont icon-dian"></i>
          <span class="zi">满99元免邮费</span>
        </li>
      </ul>

      <div class="promotion">
        <div class="promation-top">
          <div class="quanqiu">
            <div class="quan">全球换购</div>
            <span class="yijing">已满足88元享低至3.3元超值换购</span>
          </div>
          <div class="prombtn">
            <span class="again">再逛逛</span>
            <i class="iconfont icon-jiantou_liebiaoxiangyou"></i>
          </div>
        </div>
        <div class="git">
          <span class="getGift">去换购商品</span>
          <i class="iconfont icon-jiantou_liebiaoxiangyou"></i>
        </div>
      </div>

      <div v-for="(cart, index) in shopCartList" :key="index">
        <div
          class="cartgoods"
          v-for="(cartInfo, index) in cart.cartInfoList"
          :key="cartInfo.id"
        >
          <input
            class="check"
            type="checkbox"
            name="chk_list"
            
            :checked="cartInfo.isChecked"
            @click="updateOneIsCheck(cartInfo)"
          />

          <div class="goodsImg">
            <img class="Img" :src="cartInfo.imgUrl" alt="" @click="togoodesDetail" />
          </div>
          <div class="goodscar_left" v-show="!mode">
            <div class="goodsname">{{ cartInfo.skuName }}</div>
            <div class="goodsbox">
              <span class="goodschoose">语音升级款</span>
              <i class="iconfont icon-xiangxia"></i>
            </div>
            <div class="line">
              <div class="price">￥{{ cartInfo.skuPrice }}</div>
              <div class="add">
                <i
                  class="iconfont icon-jian"
                  @click="updateCartNum(-1, cartInfo)"
                ></i>
                <div class="input">
                  <input
                    class="init"
                    type="text"
                    :value="cartInfo.skuNum"
                    @change="
                      updateCartNum(
                        $event.target.value - cartInfo.skuNum,
                        cartInfo
                      )
                    "
                  />
                </div>
                <i
                  class="iconfont icon-jia"
                  @click="updateCartNum(1, cartInfo)"
                ></i>
              </div>
            </div>
          </div>
          <div class="goodscar_left_tow" v-show="mode">
            <div class="goodsbox_tow">
              <span class="goodschoose_tow">已选择:语音升级款</span>
              <i class="iconfont icon-xiangxia_tow"></i>
            </div>
            <div class="line_tow">
              <div class="price_tow">￥{{ cartInfo.skuPrice }}</div>
              <div class="add_tow">
                <i
                  class="iconfont icon-jian"
                  @click="updateCartNum(-1, cartInfo)"
                ></i>
                <div class="input_tow">
                  <input
                    class="init_tow"
                    type="text"
                    :value="cartInfo.skuNum"
                    @change="
                      updateCartNum(
                        $event.target.value - cartInfo.skuNum,
                        cartInfo
                      )
                    "
                  />
                </div>
                <i
                  class="iconfont icon-jia"
                  @click="updateCartNum(1, cartInfo)"
                ></i>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="ent"></div>
    </div>
    <div class="footer">
      <div class="checkAll">
        <div class="checkchoose">
          <input class="check2" type="checkbox" v-model="isCheckAll" />
          <span>已选{{ checkNum }}</span>
        </div>
        <div class="total">
          <span v-show="!mode">合计:{{ allMoney }}</span>
        </div>
      </div>
      <div class="footer_left">
        <button class="but " v-show="!mode" :class="checkNum &&'active'" >下单</button>
        <button class="but " v-show="mode" :class="checkNum &&'active'" @click="deleteAll">删除所选</button>
      </div>
    </div>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "ShopCart",
  data() {
    return {
      mode: false,
      count: 0,
  
    };
  },
  mounted() {
    this.getShopCartList();
  },
  methods: {
    getShopCartList() {
      this.$store.dispatch("getShopCartList");
    },
    moded() {
    

      this.mode = false;
    },
    moning(){
      this.mode=!this.mode
      // this.shopCartList.forEach((item,index) => {
      //   return item.cartInfoList.forEach((item,index)=>{
      //     item.isChecked=0
      //   })
      // });
    
    

    },

    togoodesDetail(){



    },

  
    //点击加减
    async updateCartNum(disNum, cartInfo) {
      // jia
      if (cartInfo.skuNum + disNum < 1) {
        disNum = 1 - cartInfo.skuNum;
      }
      try {
        await this.$store.dispatch("addOrUpdateShopCart", {
          skuId: cartInfo.skuId,
          skuNum: disNum,
        });
        this.getShopCartList();
      } catch (error) {
        alert("修改购物车数量失败");
      }
    },
    // 修改单个的选中状态
    async updateOneIsCheck(cartInfo) {
      try {
        await this.$store.dispatch("updateOneCartIsCheck", {
          skuId: cartInfo.skuId,
          isChecked: cartInfo.isChecked ? 0 : 1,
        });
        this.getShopCartList();
      } catch (error) {

      }
    },

  //删除
  async deleteAll() {
      let skuIdList = [];
      this.shopCartList.forEach((item) => {
        item.cartInfoList.forEach((item1) => {
          if(item1.isChecked){
            skuIdList.push(item1.skuId);
          }
        });
      });

      try {
        await this.$store.dispatch('deleteAllCart',skuIdList)
        alert("删除多个购物车成功");
        this.getShopCartList()
      } catch (error) {
        alert("删除多个购物车失败");
      }
    },





  },

  computed: {
    ...mapState({
      shopCartList: (state) => state.shopcart.shopCartList,
    }),
    // 共计总价
    allMoney() {
      return this.shopCartList.reduce((prev, item) => {
        prev += item.cartInfoList.reduce((prev1, item1) => {
          if (item1.isChecked) {
            prev1 += item1.skuNum * item1.skuPrice;
          }
          return prev1;
        }, 0);
        return prev;
      }, 0);
    },

  
  checkNum() {
      return this.shopCartList.reduce((prev, item) => {
        prev += item.cartInfoList.reduce((prev1, item1) => {
          if (item1.isChecked===1) {
            prev1+=1
          }
          return prev1;
        }, 0);
        return prev;
      }, 0);
    },

    // 选中所有状态
    isCheckAll: {
      get() {
        return this.shopCartList.every((item) => {
          return item.cartInfoList.every((item1) => {
            return item1.isChecked === 1;
          })
        }) && this.shopCartList.length
      },
      async set(val) {
        let isChecked = val ? 1 : 0;
        let skuIdList = [];
        this.shopCartList.forEach((item) => {
          item.cartInfoList.forEach((item1) => {
            skuIdList.push(item1.skuId);
          });
        });
        try {
          await this.$store.dispatch("updateAllCartIsCheck", {skuIdList,isChecked,});
          this.getShopCartList();
        } catch (error) {

        }
      },
    }, 

  },

};
</script>

<style lang="less" scoped>
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  .head {
    width: 100%;
    height: 44px;
    background-color: white;
    position: relative;
    text-align: center;
    line-height: 44px;
    .left {
      font-size: 18px;
      float: left;
      margin-left: 15px;
    }
    .right {
      position: absolute;
      top: 0;
      right: 15px;

      .take {
        display: inline-block;
        font-size: 15px;
        vertical-align: top;

        .txt {
          color: #dd1a21;
          margin-right: 10px;
        }
      }
      .cart-btn {
        font-size: 15px;
      }
    }
  }
  .body {
    flex: 1;
    width: 100%;
    background: #eeeeee;
    overflow-y: scroll;
    .list {
      width: 375px;
      height: 35px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #eeeeee;
      box-sizing: border-box;
      padding: 0 15px;
      .item {
        .zi {
          font-size: 12px;
          color: #666666;
        }
      }
    }
    .promotion {
      width: 375px;
      height: 73px;
      text-align: center;
      font-size: 12px;
      background-color: white;
      .promation-top {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 10px;
        .quanqiu {
          .quan {
            display: inline-block;
            font-size: 12px;
            color: white;
            width: 56px;
            height: 20px;
            background-color: #fc865f;
            border-radius: 5px;
            line-height: 20px;
            text-align: center;
            margin-right: 5px;
          }
          .yijing {
            font-size: 14px;
          }
        }
        .prombtn {
          .again {
            font-size: 12px;
            color: #dd1a21;
          }
        }
      }
      .git {
        box-sizing: border-box;
        width: 317px;
        height: 28px;
        background-color: #fff8f7;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 0 15px 5px 30px;
        .getGift {
          font-size: 12px;
        }
        .iconfont {
          font-size: 20px;
        }
      }
    }
    .cartgoods {
      width: 375px;
      height: 125px;
      display: flex;
      align-items: center;
      margin-top: 15px;
      margin-bottom: 15px;
      background-color: white;
      position: relative;
      .check {
        margin: 0 10px;
        width: 15px;
        height: 15px;
        border: 1px solid #bbb;
        // border-radius: 50%;
      }
      .goodsImg {
        background-color: #bbb;
        .Img {
          width: 86px;
          height: 86px;
        }
      }
      .goodscar_left {
        width: 221px;
        height: 86px;
        margin: 0 15px;

        .goodsname {
          font-size: 14px;
          color: #333333;
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2; /** 多行文本溢出...*/
        }
        .goodsbox {
          height: 20px;
          border: 1px solid #bbb;
          margin-top: 5px;
          display: flex;
          align-items: center;
          display: inline-block;

          .goodschoose {
            color: #7f7f7f;
            font-size: 14px;
          }
          .iconfont {
            font-size: 12px;
            color: #7f7f7f;
          }
        }

        .line {
          display: flex;
          align-items: center;
          justify-content: space-between;

          &.active {
            position: absolute;
            bottom: 1px;
            display: flex;
            align-items: center;
          }
          .add {
            height: 26px;
            margin-bottom: 10px;
            display: flex;
            align-content: center;
            .iconfont {
              border: 1px solid #bbb;
              height: 26px;
              width: 29px;
              text-align: center;
              line-height: 26px;
            }

            .input {
              width: 29px;
              height: 26px;
              border: 1px solid #bbb;

              .init {
                width: 29px;
                height: 26px;
                text-align: center;
                box-sizing: border-box;
                border: none;
              }
            }
          }
        }
      }
      .goodscar_left_tow {
        width: 221px;
        height: 86px;
        margin: 0 15px;

        .goodsbox_tow {
          text-align: right;
          .goodschoose_tow {
            color: #7f7f7f;
            font-size: 14px;
          }
          .iconfont_tow {
            font-size: 12px;
            color: #7f7f7f;
          }
        }

        .line_tow {
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-top: 5px;
          .add_tow {
            height: 26px;
            margin-bottom: 10px;
            display: flex;
            align-content: center;
            .iconfont {
              border: 1px solid #bbb;
              height: 26px;
              width: 29px;
              text-align: center;
              line-height: 26px;
            }

            .input_tow {
              width: 29px;
              height: 26px;
              border: 1px solid #bbb;

              .init_tow {
                width: 29px;
                height: 26px;
                text-align: center;
                box-sizing: border-box;
                border: none;
              }
            }
          }
        }
      }
    }
    .ent {
      height: 59px;
    }
  }
  .footer {
    width: 100%;
    height: 59px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-direction: row;
    .checkAll {
      height: 59px;
      width: 262px;
      text-align: center;
      line-height: 59px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-right: 10px;
      .checkchoose {
        margin-left: 15px;
        color: #bbb;
        .check2 {
          margin: 0 10px;
          width: 15px;
          height: 15px;
          border: 1px solid #bbb;
        }
      }
      .total {
        font-size: 14px;
        color: #dd1a21;
      }
    }
    .footer_left {
      .but {
        height: 59px;
        width: 113px;
        border: none;
        display: inline-block;
        background-color: #ccc;
        color: #fff;
        font-size: 14px;
        vertical-align: middle;
        box-sizing: border-box;
        overflow: hidden;
        vertical-align: middle;
        &.active{
        background-color:#dd1a21
      }
      }
      
    
    }
  }
}
</style>
